package com.anbinh.doctor.utils;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.anbinh.doctor.R;

public class DateTimePicker {
	private Activity mActivity;
	private int month ;
	public DateTimePicker(Activity activity) {
		mActivity=activity;
	}
	public void addViewDate(final LinearLayout mLineTime, final int mMonth,
			int mDay,final IRcallBak callBak) {
		month=mMonth;
		int year = Integer.parseInt(Utils.getCurrentYear());
		if (mMonth < 0) {
			month = Integer.parseInt(Utils.getCurrentMonth());
		}

		int lastDay = Integer.parseInt(Utils.getLastDay(month, year));

		mLineTime.removeAllViews();
		DisplayMetrics displaymetrics = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay()
				.getMetrics(displaymetrics);
		int widthItem = displaymetrics.widthPixels / 8;

		for (int i = 0; i < lastDay; i++) {
			View view = mActivity.getLayoutInflater().inflate(
					R.layout.item_date_time, null);
			view.setTag(i+1);
			LayoutParams params = new LayoutParams(widthItem,
					LayoutParams.MATCH_PARENT);
			view.setLayoutParams(params);

			TextView day = (TextView) view.findViewById(R.id.dayName);
			String dayName = Utils.getDayName(
					Integer.parseInt(Utils.getCurrentYear()), month, (i + 1));
			day.setText(dayName.substring(0, 1));

			TextView date = (TextView) view.findViewById(R.id.date);
			date.setText((i + 1) + "");
			if (i != mDay) {
				date.setBackgroundResource(android.R.color.transparent);
			}else{
				date.setTextColor(Color.parseColor("#ffffff"));
			}

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					for (int i = 0; i < ((ViewGroup) mLineTime).getChildCount(); ++i) {
						View nextChild = ((ViewGroup) mLineTime).getChildAt(i);
						if (nextChild.getTag().equals(v.getTag())) {
							TextView child =(TextView) ((ViewGroup) nextChild).getChildAt(1);
							child.setBackgroundResource(R.drawable.date_active);
							child.setTextColor(Color.parseColor("#ffffff"));
							Bundle bundle=new Bundle();
							bundle.putString(Utils.TIME, Utils.getCurrentYear()+"/"+month+"/"+v.getTag());
									
							callBak.onDisplay(1, bundle);
						} else {
							TextView child = (TextView)((ViewGroup) nextChild).getChildAt(1);
							child.setBackgroundResource(android.R.color.transparent);
							child.setTextColor(Color.parseColor("#15c3e8"));
						}
					}

				}
			});
			mLineTime.addView(view);
		}
	}
}
